#include <cnet.h>

//----------------cnet includes------------------
#include <cnet.h>
//-----------------------------------------------



//-------------------sending---------------------
#include "link_layer/dl_sending.c"
#include "network_layer/nw_sending.c"
//-----------------------------------------------



//------------------receiving--------------------
#include "network_layer/nw_receiving.c"
#include "link_layer/dl_receiving.c"
//-----------------------------------------------



//function called on node startup
void reboot_node(CnetEvent ev, CnetTimerID timer, CnetData data)
{	
    reboot_timer =1;
    CNET_start_timer(EV_TIMER2,reboot_timer,0);

	/*
	 *-------------<<<<HANDLER>>>>-------------
	 */
	//send handler
	CHECK(CNET_set_handler(EV_APPLICATIONREADY, NW_new_msg, 0));
	
	//receive handler
	CHECK(CNET_set_handler(EV_PHYSICALREADY, DL_receive_frame, 0));
	
	//timeout handler
	CHECK(CNET_set_handler( EV_TIMER1, DL_timeouts, 0));
	
	CHECK(CNET_set_handler(EV_TIMER2,NW_reboot_timeouts,0));
	
	/*
	 *---------------<<<<INIT>>>>---------------
	 */
	
	
	/*
	 * initializing OUTGOING link data array
	 */
	a_out_link_data = (OUT_LINK_DATA*) calloc(nodeinfo.nlinks, sizeof(OUT_LINK_DATA));
	for(unsigned int link = 1; link <= nodeinfo.nlinks; ++link) {
		
		OUT_LINK_DATA* iter =	&a_out_link_data[link - 1];
		iter->infolen =			0;			
		iter->s_max_chunk =		0;		
		iter->c_curr_part =		0;		
		iter->f_pkt_finished =	1;		
		iter->f_link_ready =	1;		
		iter->c_total_parts =	0;		
		iter->s_last_chunk =	0;		
		iter->buffer = queue_new();
		iter->S_BUFFER =		1000; //might be determined dynamically (e.g., based on throughput, mtu, ...)
		iter->lasttimer =		NULLTIMER;
		
	}
	
	/*
	 * initializing INCOMING link data array
	 */
	a_inc_link_data = (INC_LINK_DATA*) calloc(nodeinfo.nlinks, sizeof(INC_LINK_DATA));
	for(unsigned int link = 1; link <= nodeinfo.nlinks; ++link) {
		
		INC_LINK_DATA* iter =	&a_inc_link_data[link - 1];
		iter->c_curr_part = 0;
		iter->infolen = 0;
		iter->s_max_chunk = linkinfo[link].mtu - FRAME_HEADER_SIZE;

		
	}
	

	
}
